<?php 
namespace town\model;

use town\dmod\DB;
use town\dmod\Model;


/**
 * 标签信息
 */
class Tags extends Model
{

	/**
	 * 获取tag的字典
	 */
	public function dict()
	{
		static $dict = [];
		if (count($dict)) {
			return $dict;
		}
		$datas = DB::table('tags')->get();
		foreach ($datas as $key => $value) {
			$dict[$value['id']] = $value['name'];
		};
		return $dict;
	}


	/**
	 * 通过postId 获取标签列表
	 */
	public function tagByPostId($id)
	{
		$tags = DB::table('tags_map_posts')->where('pid',$id)->get();
		$dict = $this->dict();
		foreach ($tags as $key => &$tag) {
			$tag['name'] = $dict[$tag['tid']];
		}
		return $tags;
	}
}